<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------


namespace app\api\controller;

use app\api\lists\CardLists;
use app\api\logic\DashboardLogic;
use app\api\logic\RechargeLogic;
use app\api\validate\DashboardValidate;
use app\api\validate\RechargeValidate;
use app\common\enum\PayEnum;
use app\common\enum\user\AccountLogEnum;
use app\common\logic\AccountLogLogic;
use app\common\model\recharge\RechargeOrder;
use app\common\model\user\User;
use app\common\model\user\UserGroup;
use think\facade\Config;



class DashboardController extends BaseApiController
{

    public function lists()
    {
        if (isset($_GET['export']) && $_GET['export'] == 2) {
            $user = User::findOrEmpty($this->userInfo['user_id']);
            if ($user->isEmpty()) {
                return $this->fail('用户不存在');
            }
            $passwordSalt = Config::get('project.unique_identification');
            $oldPassword = create_password($_GET['secure_password'], $passwordSalt);
            if ($oldPassword != $user['secure_password']) {
                return $this->fail('交易密码不正确');
            }
        }
        return $this->dataLists(new CardLists());
    }


    /**
     * @notes 添加card
     * @return \think\response\Json
     * @author likeadmin
     * @date 2025/04/09 19:15
     */
    public function add()
    {
        $params = (new CardValidate())->post()->goCheck('add');
        $params['user_info'] = $this->userInfo;
        $result = CardLogic::add($params);
        if (true === $result) {
            return $this->success('开卡成功', [], 1, 1);
        }

        return $this->fail(CardLogic::getError());
    }


    /**
     * @notes 编辑card
     * @return \think\response\Json
     * @author likeadmin
     * @date 2025/04/09 19:15
     */
    public function edit()
    {
        $params = (new CardValidate())->post()->goCheck('edit');
        $params['group_id'] = $this->userInfo['group_id'];
        $result = CardLogic::edit($params);
        if (true === $result) {
            return $this->success('编辑成功', [], 1, 1);
        }
        return $this->fail(CardLogic::getError());
    }


    /**
     * @notes 删除card
     * @return \think\response\Json
     * @author likeadmin
     * @date 2025/04/09 19:15
     */
    public function delete()
    {
        $params = (new CardValidate())->post()->goCheck('delete');
        $params['group_id'] = $this->userInfo['group_id'];
        $result = CardLogic::delete($params);
        if (true === $result) {
            return $this->success('删除成功', [], 1, 1);
        }
        return $this->fail(CardLogic::getError());
    }

    public function change_user()
    {
        $params = (new CardValidate())->post()->goCheck('change');
        $params['group_id'] = $this->userInfo['group_id'];
        $result = CardLogic::change($params);
        if (true === $result) {
            return $this->success('修改成功', [], 1, 1);
        }
        return $this->fail(CardLogic::getError());
    }


    public function recharge()
    {
        $params = (new RechargeValidate())->post()->goCheck('recharge', [
            'user_id' => $this->userId,
            'terminal' => $this->userInfo['terminal'],
        ]);
        $params['user_info'] = $this->userInfo;

        // 增加用户累计充值金额及用户余额
        $result = CardLogic::recharge($params);
        if (true !== $result) {
            return $this->fail(CardLogic::getError());
        }

        return $this->success('充值成功', [], 1, 1);
    }


    public function daily_spend()
    {
        $params = (new DashboardValidate())->post()->goCheck('daily');
        // 获取日期范围
        $params['between_time'] = DashboardValidate::getDateRange($params['option']);
        $params['group_id'] = $this->userInfo['group_id'];
        $result = DashboardLogic::dailySpend($params);
        return $this->success('SUCCESS', $result, 1, 1);
    }

    public function index(){
        $params['group_id'] = $this->userInfo['group_id'];
        $result = DashboardLogic::index($params);
        return $this->success('SUCCESS', $result, 1, 1);
    }

    public function declined_rate(){
        $params = (new DashboardValidate())->post()->goCheck('daily');
        // 获取日期范围
        $params['between_time'] = DashboardValidate::getDateRange($params['option']);
        $params['group_id'] = $this->userInfo['group_id'];
        $result = DashboardLogic::declinedRate($params);
        return $this->success('SUCCESS', $result, 1, 1);
    }


}